package pr.iceworld.fernando.springbatchlearning.configuration;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import pr.iceworld.fernando.springbatchlearning.entity.Users;
import pr.iceworld.fernando.springbatchlearning.listener.JobListener;
import pr.iceworld.fernando.springbatchlearning.repository.UserMapper;
import pr.iceworld.fernando.springbatchlearning.service.ProcessService;
import pr.iceworld.fernando.springbatchlearning.service.ReaderService;
import pr.iceworld.fernando.springbatchlearning.service.WriterService;

import javax.annotation.Resource;
import java.util.Date;

@Configuration
@EnableBatchProcessing
public class MyBatchConfig {

    private final static Logger log = LoggerFactory.getLogger(MyBatchConfig.class);
    @Autowired
    public JobLauncher jobLauncher;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Resource
    ReaderService readerService;
    @Resource
    private ProcessService processService;
    @Resource
    private WriterService writerService;
    @Resource
    private UserMapper userMapper;
    @Autowired
    private JobListener jobListener;

    public void run() {
        try {
            String dateParam = new Date().toString();
            JobParameters param =    new JobParametersBuilder().addString("date", dateParam).toJobParameters();
            System.out.println(dateParam);
            JobExecution execution = jobLauncher.run(dataHandleJob(), param);             //执行job
            System.out.println("Exit Status : " + execution.getStatus());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private EntityWrapper<Users> initEntityWrapper() {
        EntityWrapper<Users> entityWrapper = new EntityWrapper<>();
        entityWrapper.le("age", 20);
        System.out.println(entityWrapper.getSqlSegment());
        return entityWrapper;
    }

//    @Bean
    public Job dataHandleJob() {
        System.out.println("size ================= " + userMapper.selectCount(initEntityWrapper()));

        return jobBuilderFactory.get("dataHandleJob")
                .incrementer(new RunIdIncrementer())
                .start(handleDataStep())
                .listener(jobListener)
                .build();
    }

    @Bean
    public Step handleDataStep() {
        return stepBuilderFactory.get("step")
                .<Users, Users> chunk(50)
                .faultTolerant().retryLimit(3).retry(Exception.class).skipLimit(100).skip(Exception.class)
                .reader(readerService)
                .processor(processService)
                .writer(writerService)
                .build();
    }

}
